// pages/recard/recard.js
import { parking_record_userlist, vehicle_info } from "../../api/http"
const util = require('../../utils/util.js')
const { formatTime2 } = util
const timeFn = (times)=> {
   let hours = Math.floor(times / 3600)
   let minutes = Math.floor(times % 3600 / 60)
   let seconds = Math.floor(times % 60)
   return `${hours}小时${minutes}分钟${seconds}秒`
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    arr: [],
    parkRecards: []
  },

  // 获取车辆信息
  async vehicle_infoFn(){
    let token = wx.getStorageSync('token');
    if(!token) {
      wx.showToast({
        icon: 'none',
        title: '尚未登录哦~',
      })
      wx.showModal({
        title: '提示',
        content: '是否跳转登录~~',
        complete: (res) => {
          if (res.cancel) {
            
          }
      
          if (res.confirm) {
              wx.redirectTo({
                url: '../login/login',
              })
          }
        }
      })
      return false;
    }
    await vehicle_info({token}).then(
      content=>{
        //  console.log(content);
         let {code , data} = content;
         console.log("车牌列表:",data)
         if(code == 200){
            this.setData({
              arr: data
          })
         }
         
      }
    )
  },

   // 获取停车记录
   async recard_infoFn(){
    let token = wx.getStorageSync('token');
    if(!token) {
      wx.showToast({
        icon: 'none',
        title: '尚未登录哦~',
      })
      wx.showModal({
        title: '提示',
        content: '是否跳转登录~~',
        complete: (res) => {
          if (res.cancel) {
            
          }
      
          if (res.confirm) {
              wx.redirectTo({
                url: '../login/login',
              })
          }
        }
      })
      return false;
    }
    // 存储所有车牌的停车记录
    let newArr = [];
    let arr = this.data.arr;
    for(let i = 0 ; i < arr.length ; i ++){
        let vehicle_num = arr[i].vehicle_num
        await parking_record_userlist({token,vehicle_num}).then(
          content=>{
            let {code , result} = content;
            if(code == 200){
              // console.log('停车记录: ',result); 
              result.forEach(item=>{
                 newArr.push(item);
              })
            }
          }
        )
    }
    // console.log("x:",newArr)
    let arr2024 = newArr.map(item=>{
        let obj = {};
        let str = item.parking_record_cost.toString();
        obj.parking_record_cost = str.indexOf(".") >  -1 ? str : str + ".00";
        obj.vehicle_num = item.vehicle_num;
        obj.parking_record_enter_time = formatTime2(item.parking_record_enter_time);
        obj.parking_record_leave_time = formatTime2(item.parking_record_leave_time);
        let s = new Date(item.parking_record_enter_time).getTime();
        let e = new Date(item.parking_record_leave_time).getTime();
        let times = (e - s) / 1000;
        obj.times = timeFn(times);
        return obj;
    })
    this.setData({
      parkRecards: arr2024
    })
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
   async onLoad(options) {
    // 获取车辆信息
    await this.vehicle_infoFn();
    // 获取该用户的所有停车记录
    this.recard_infoFn();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})